package com.workcell.workdata.xthings.controller;

import com.github.pagehelper.PageInfo;
import com.workcell.workdata.common.core.util.R;
import com.workcell.workdata.xthings.dto.ManufacturerDto;
import com.workcell.workdata.xthings.dto.ManufacturerVo;
import com.workcell.workdata.xthings.dto.PageVo;
import com.workcell.workdata.xthings.entity.Manufacturer;
import com.workcell.workdata.xthings.service.ManufacturerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.util.List;

@Api(value = "ManufacturerController", tags = "厂商管理")
@RestController
@RequestMapping("/manufacturer")
@RequiredArgsConstructor
public class ManufacturerController {
    private final ManufacturerService manufacturerService;

    @ApiOperation("厂商列表")
    @GetMapping("/list")
    public R<List<ManufacturerDto>> manufacturerList(ManufacturerVo manufacturerVo) {
        List<ManufacturerDto> list = manufacturerService.manufacturerList(manufacturerVo);
        return R.ok(list);
    }

    @ApiOperation("厂商分页")
    @GetMapping("/page")
    public R<PageInfo<ManufacturerDto>> manufacturerPage(PageVo pageVo, ManufacturerVo manufacturerVo) {
        PageInfo<ManufacturerDto> pageInfo = manufacturerService.manufacturerPage(pageVo, manufacturerVo);
        return R.ok(pageInfo);
    }

    @ApiOperation("厂商保存")
    @PostMapping("/insert")
    public R<Boolean> insert(@Valid @RequestBody ManufacturerVo manufacturerVo) {
        return R.ok(manufacturerService.insert(manufacturerVo));
    }

    @ApiOperation("厂商删除")
    @DeleteMapping("/delete/{manufacturerCode}")
    public R<Boolean> delete(@Valid @NotEmpty(message = "厂商ID不能为空")
                                 @PathVariable("manufacturerCode") String manufacturerCode) {
        return R.ok(manufacturerService.delete(manufacturerCode));
    }

    @ApiOperation("厂商-下拉列表")
    @GetMapping("/selectList")
    public R<List<Manufacturer>> selectList(ManufacturerVo manufacturerVo) {
        return R.ok(manufacturerService.selectList(manufacturerVo));
    }
}
